//
//  WBStatusViewModel.swift
//  WB123
//
//  Created by xuyunde mac pro on 2017/7/4.
//  Copyright © 2017年 NSObjiect. All rights reserved.
//

import UIKit

class WBStatusViewModel: NSObject {

    // 模型
    var homeModel: WBHomeModel?{
        didSet{
            // 认证
            verifiedImage = dealVerifiedImage(verified: homeModel?.user?.verified ?? -1)
            // 会员等级
            mbrankImage = dealMbrankImage(mbrank: homeModel?.user?.mbrank ?? 0)
            // 微博来源
            sourceText = dealSinaSource(source: homeModel?.source)
        }
    }
    // 认证图片
    var  verifiedImage: UIImage?
    /// 会员等级
    var mbrankImage: UIImage?
    /// 微博时间
    var createdAtText: String?{
        return dealSinaTime(created_at: homeModel?.created_at)
    }
    // 微博来源
    var sourceText: String?
    

}
extension WBStatusViewModel{
    //MARK: - 处理微博来源
    fileprivate func dealSinaSource(source: String?) -> String?{
        // 判断是否为nil 且是否保存">
        guard let s = source, s.contains("\">") else {
            return "来自 黑马25期微博"
        }
        
        // 获取起始
        let startIndex = s.range(of: "\">")!
        // 获取结束
        let endIndex = s.range(of: "</")!
        //lowerBound 底左侧 upperBound 高 右侧 (升序 从左到右)
        let result = s.substring(with: startIndex.upperBound..<endIndex.lowerBound)
        return "来自 " + result
    }
}


extension WBStatusViewModel{
    
    // 处理微博时间
    fileprivate func dealSinaTime(created_at: Date?) -> String?{
        // 是否为nil
        guard let d = created_at else {
            return nil
        }
        // 测试数据
//        let d = d1.addingTimeInterval(-3600*2)
        // 定义时间格式化
        let df = DateFormatter()
        // 记录日期格式字符串
        let dateFormatString: String
        
        // 判断是否今年
        if isThisYear(sinaDate: d) {
            
            // 日历类
            let calendar = Calendar.current
            // 如果是今天
            if calendar.isDateInToday(d) {
                let s = Int(-d.timeIntervalSinceNow)
                print(s)
                // 显示刚刚
                if s < 60 {
                    return "刚刚"
                }else if s >= 60 && s < 3600{
                    return "\(s/60)分钟前"
                }else{
                    return "\(s/3600)小时前"
                }
                
            }else if calendar.isDateInYesterday(d){
                // 昨天
                dateFormatString = "昨天 HH:mm"
            }else{
                // 其他
                dateFormatString = "MM月dd日 HH:mm"
                
            }
            
        }else{
            
            print("其他年")
            
            // 指定格式
            dateFormatString = "yyyy年MM月dd日 HH:mm"
        }
        // 指定格式
        df.dateFormat = dateFormatString
        return df.string(from: d)
    }
    fileprivate func isThisYear(sinaDate: Date) -> Bool{
        //定义时间格式化
        let df = DateFormatter()
        // 指定格式
        df.dateFormat = "yyyy"
        // 当前系统时间
        let currentDate = Date()
        // 通过日期获取日期字符串
        
        //新浪微博
        let sinaString = df.string(from: sinaDate)
        // 当前时间
        let currentString = df.string(from: currentDate)
        return sinaString == currentString
    }
 
}

extension WBStatusViewModel{
    
    // MARK: -处理会员业务逻辑  1-6等级
    fileprivate func dealMbrankImage(mbrank: Int) -> UIImage?{
        
        if mbrank >= 1 && mbrank <= 6 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        return UIImage(named: "common_icon_membership")
    }

    //处理用户认证图片
    // 认证类型 -1 :没有认证1，认证用户，2,3,5: 企业认证，220: 达人
    fileprivate func dealVerifiedImage(verified: Int) -> UIImage?{
        
        
        var imgName = ""
        switch verified {
        case 1:
            imgName = "avatar_vip"
        case 2,3,5:
            imgName = "avatar_enterprise_vip"
        case 220:
            imgName = "avatar_grassroot"
        default:
            imgName = "avatar_vgirl"
        }
        return UIImage(named: imgName)
    }
}
